package com.lhm.dao;


import com.lhm.entity.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;



@Repository
public class UserDao {

    @Autowired
    MongoTemplate mongoTemplate;

    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    public User findUserName( String username){
        Query query= new Query();
        query.addCriteria(Criteria.where("username").is(username));

        User user=mongoTemplate.findOne(query,User.class);
            return user;
    }

    public void add(User u) {
        String pass = u.getPassword();
        if (pass != null || pass.length() > 1) {
            u.setPassword(this.passwordEncoder.encode(pass));
        }

        mongoTemplate.save(u);
    }


}
